<script>
	import {
		getUserInfo
	} from "@/api/members";
	import {
		getRegionsById,
		publicWeb
	} from "@/api/common.js";
	import storage from "@/utils/storage";
	import config from '@/config/config'
	export default {
		onLaunch: function(options) {
			//#ifdef MP-WEIXIN
			this.getCurrentLocation()
			//#endif

			//#ifdef MP-ALIPAY
			// my.alert({
			// 	content: JSON.stringify()
			// })
			if (options.query) {
				uni.setStorageSync('pay_sn', options.query.sn)
				uni.setStorageSync('goods_id', options.query.id)
			}
			//#endif
			const token = storage.getAccessToken();
			token && this.getInfo()
			console.log('App Launch')
			this.getRegionsById()
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 获取当前定位
			getCurrentLocation() {
				// 1. 检查是否授权定位
				uni.getSetting({
					success: (settingRes) => {
						// 2. 未授权：申请授权
						if (!settingRes.authSetting['scope.userLocation']) {
							uni.authorize({
								scope: 'scope.userLocation',
								success: () => {
									// 授权成功后，调用定位 API
									this.fetchLocation();
								},
								fail: () => {
									// 授权失败：引导用户手动开启
									uni.showModal({
										title: '权限不足',
										content: '请在设置中开启定位权限',
										confirmText: '去设置',
										success: (modalRes) => {
											if (modalRes.confirm) {
												// 打开小程序设置页面
												uni.openSetting({
													success: (openRes) => {
														if (openRes.authSetting['scope.userLocation']) {
															this.fetchLocation(); // 用户开启后重新获取
														}
													}
												});
											}
										}
									});
								}
							});
						} else {
							// 已授权：直接调用定位 API
							this.fetchLocation();
						}
					}
				});
			},
			// 调用定位 API 获取经纬度
			fetchLocation() {
				uni.getLocation({
					type: 'gcj02', // 微信小程序推荐使用国测局坐标系
					success: (res) => {
						console.log('定位成功', res);
						// this.locationInfo = {
						// 	latitude: res.latitude, // 纬度
						// 	longitude: res.longitude, // 经度
						// 	address: '' // 后续解析地址
						// };
						// 可选：解析经纬度为具体地址（需调用地图逆地理编码 API）
						this.getAddress(res.latitude, res.longitude);
					},
					fail: (err) => {
						console.error('定位失败', err);
						uni.showToast({
							title: '获取定位失败，请重试',
							icon: 'none'
						});
					}
				});
			},
			getAddress(lat, lng) {
				uni.request({
					url: `https://apis.map.qq.com/ws/geocoder/v1`,
					method: "GET",
					data: {
						key: config.aMapKey, //web服务的key
						location: `${lat},${lng}`,
					},
					success: (res) => {
						console.log('当前位置', res)
						const result = res.data.result
						const address = {
							label: result.address_component.city,
							location: result.location
						}
						uni.setStorageSync("selectCurrentAddress", address)
						this.$store.commit('setcurrentAddress', address)
					},
					fail: (err) => {

					},
				});
			},
			getRegionsById() {
				uni.showLoading()
				getRegionsById().then((res) => {
					console.log(res)
					uni.setStorageSync('areaTree', res.data)
				}).finally(() => {
					uni.hideLoading()
				});
			},
			getInfo() {
				getUserInfo().then((res) => {
					console.log(res)
					const data = res.data
					if (data) {
						storage.setAccessToken(data.token);
						/**
						 * 个人信息存储到缓存userInfo中
						 */
						storage.setUserInfo(data);
						storage.setHasLogin(true);
					}
				});
			},
		}
	}
</script>

<style>
	/*每个页面公共css */
	@import '@/style/free.css';
	@import '@/style/common.scss';
	@import '@/style/size.css';
	@import '@/style/color.css';
</style>